<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> -->
    <meta name="viewport" content="user-scalable=0">
    <meta content="width=device-width,user-scalable=no" name="viewport">
    <link rel="shortcut icon" href="img/favicon.ico">
    <link rel="stylesheet" type="text/css" href="layui/css/layui.css">
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <script type="text/javascript" src="layui/layui.js"></script>
    <script type="text/javascript" src="js/index.js" defer></script>
    <title>口袋莫蒂IV计算器</title>
</head>
<body>
    <div class = "container">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>IV 计算器</legend>
        </fieldset>
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">莫蒂: </label>
                <div class="layui-input-block">
                  <select name="morty" lay-filter="morty" lay-search lay-verify="required" id = "morty">
                    <option value="">请输入查找的莫蒂</option>
                    <!-- <option value="0" selected>#001 - 莫蒂</option>
                    <option value="1">#002 - 邋遢莫蒂</option>
                    <option value="2">#003 - 邋遢莫蒂</option>
                    <option value="3">#004 - 乞丐莫蒂</option> -->
                  </select>
                </div>
              </div>
              <div class="layui-form-item">
                <label class="layui-form-label">计算类型: </label>
                <div class="layui-input-block">
                  <input type="radio" name="type" value="min" title="Min EVs(0)" checked>
                  <input type="radio" name="type" value="max" title="Max EVs(65535)">
                  <input type="radio" name="type" value="custom" title="Custom EVs" disabled>
                </div>
              </div>
            <div class="layui-form-item">
              <label class="layui-form-label">等级: </label>
              <div class="layui-input-block">
                <input type="text" name="lvl" lay-verify="required" autocomplete="off" placeholder="请输入等级" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">体力值: </label>
              <div class="layui-input-block">
                <input type="text" name="hp" lay-verify="required" autocomplete="off" placeholder="请输入体力值" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">攻击: </label>
              <div class="layui-input-block">
                <input type="text" name="atk" lay-verify="required" autocomplete="off" placeholder="请输入攻击值" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">防御: </label>
              <div class="layui-input-block">
                <input type="text" name="def" lay-verify="required" autocomplete="off" placeholder="请输入防御值" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">速度: </label>
              <div class="layui-input-block">
                <input type="text" name="spd" lay-verify="required" autocomplete="off" placeholder="请输入速度值" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">体力值IV: </label>
              <div class="layui-input-block">
                <input type="text" name="hp_iv" lay-verify="title" autocomplete="off" class="layui-input result" readOnly="true" id = "hp_iv">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">攻击值IV: </label>
              <div class="layui-input-block result">
                <input type="text" name="atk_iv" lay-verify="title" autocomplete="off" class="layui-input result" readOnly="true" id = "atk_iv">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">防御值IV: </label>
              <div class="layui-input-block result">
                <input type="text" name="def_iv" lay-verify="title" autocomplete="off" class="layui-input result" readOnly="true" id = "def_iv">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">速度值IV: </label>
              <div class="layui-input-block result">
                <input type="text" name="spd_iv" lay-verify="title" autocomplete="off" class="layui-input result" readOnly="true" id = "spd_iv">
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit" id = "submit">计算</button>
                <button type="reset" class="layui-btn layui-btn-primary">清空</button>
              </div>
            </div>
        </form>
    </div>
</body>
<script>
  layui.use(['form', 'jquery'], function(){
    var form = layui.form;
    var $ = layui.jquery;
    //计算iv函数
    var calculate = function(data, type) {
    var iv_lt = 0, iv_rt = 16;
    var flag1 = false, flag2 = false;
    var base = zhongzhu[data.field.morty][type];
    var x, y;
    for (var i = 0; i <= 16; i ++ ) {
      if (type == 0) {
        if (data.field.type == "min") {
          x = parseInt((base + i + 50) * data.field.lvl / 50) + 10;
          y = parseInt((base + 16 - i + 50) * data.field.lvl / 50) + 10;
        } else if (data.field.type == "max") {
          x = parseInt((base + i + parseInt(Math.sqrt(65535) / 4) + 50) * data.field.lvl / 50) + 10;
          y = parseInt((base + 16 - i + parseInt(Math.sqrt(65535) / 4) + 50) * data.field.lvl / 50) + 10;
        } else {

        }
      } else {
        if (data.field.type == "min") {
          x = parseInt((base + i) * data.field.lvl / 50) + 5;
          y = parseInt((base + 16 - i) * data.field.lvl / 50) + 5;
        } else if (data.field.type == "max") {
          x = parseInt((base + i + parseInt(Math.sqrt(65535) / 4)) * data.field.lvl / 50) + 5;
          y = parseInt((base + 16 - i + parseInt(Math.sqrt(65535) / 4)) * data.field.lvl / 50) + 5;
        } else {

        }
      }
      if (!flag1) {
        if (type == 0) {
          if (data.field.hp == x) {
            flag1 = true;
            iv_lt = i;
          }
        } else if (type == 1) {
          if (data.field.atk == x) {
            flag1 = true;
            iv_lt = i;
          }
        } else if (type == 2) {
          if (data.field.def == x) {
            flag1 = true;
            iv_lt = i;
          }
        } else {
          if (data.field.spd == x) {
            flag1 = true;
            iv_lt = i;
          }
        }
      }
      if (!flag2) {
        if (type == 0) {
          if (data.field.hp == y) {
            flag2 = true;
            iv_rt = 16 - i;
          }
        } else if (type == 1) {
          if (data.field.atk == y) {
            flag2 = true;
            iv_rt = 16 - i;
          }
        } else if (type == 2) {
          if (data.field.def == y) {
            flag2 = true;
            iv_rt = 16 - i;
          }
        } else {
          if (data.field.spd == y) {
            flag2 = true;
            iv_rt = 16 - i;
          }
        }
      }
    }
    if (!flag1 || !flag2) {
      if (type == 0) $("#hp_iv").val("No IV found");
      else if (type == 1) $("#atk_iv").val("No IV found");
      else if (type == 2) $("#def_iv").val("No IV found");
      else $("#spd_iv").val("No IV found");
    } else if (iv_lt == iv_rt) {
      if (type == 0) $("#hp_iv").val(iv_lt);
      else if (type == 1) $("#atk_iv").val(iv_lt);
      else if (type == 2) $("#def_iv").val(iv_lt);
      else $("#spd_iv").val(iv_lt);
    } else {
      if (type == 0) $("#hp_iv").val(iv_lt + " - " + iv_rt);
      else if (type == 1) $("#atk_iv").val(iv_lt + " - " + iv_rt);
      else if (type == 2) $("#def_iv").val(iv_lt + " - " + iv_rt);
      else $("#spd_iv").val(iv_lt + " - " + iv_rt);
    }
}
    //初始化读取莫蒂列表
    $(function(){ 
      //声明
      layer.open({
        type: 1,
        shade: false,
        title: "使用须知",
        content: "<div style='height:275px;width:300px;padding:15px;line-height:30px;'>" + 
                  "<h5>本网站是为了方便口袋莫蒂的同好们进行IV查询</h5>" + 
                  "<p>戈创数值已更新</p>" +
                  "<p>1. 口袋莫蒂同好迎新群群号：909497572</p>" +
                  "<p>2. 口袋莫蒂同好吹水群群号：362553352</p>" +
                  "<p>3. 口袋莫蒂同好交易群群号：810813581</p>" +
                  "<p>4. 口袋莫蒂同好互刷群群号：936146877</p>" +
                  "<p>进群先看群公告！进群先看群公告！进群先看群公告！重要的事情说3遍</p>" +
                  "<p style='font-size:12px;'>版权归moxiaoxia所有，如有疑问请联系QQ：1151418758</p>" +
                "</div>"
      });
      //获取莫蒂数量
      var length = 0;
      for (var x in zhongzhu) length ++ ;
      //动态添加option元素
      //$("#morty").append("<option value = '0' id = 'n1'>#1 - 莫蒂</option>");
      for (var i = 0; i < length; i ++ ) {
        $("#morty").append("<option value = '"+ i +"'>#"+ (i + 1) +" - "+ zhongzhu[i][4] +"</option>");
      }
      //重新渲染
      form.render();
    }); 
    //监听提交
    form.on('submit(submit)', function(data){
      console.log(data.field);
      if (isNaN(data.field.lvl) || isNaN(data.field.hp) || isNaN(data.field.atk) || isNaN(data.field.def) || isNaN(data.field.spd)) layer.msg("请全部输入数字,不要输入字符！！");
      else if (data.field.lvl <= 0 || data.field.lvl > 100 || data.field.hp <= 0 || data.field.atk <= 0 || data.field.def <= 0 || data.field.spd <= 0) layer.msg("输入无效数字，请重新检查输入！！");
      else {
        calculate(data, 0);
        calculate(data, 1);
        calculate(data, 2);
        calculate(data, 3);
      }
      return false;
      });
    }); 
</script>
</html>